package com.fg.cloud.manager.service.system.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.construction.Select;
import com.fg.cloud.manager.entry.system.InvoiceManageEntity;
import com.fg.cloud.manager.mapper.system.InvoiceManageMapper;
import com.fg.cloud.manager.service.system.InvoiceManageService;

@Service
public class InvoiceManageServiceImpl implements InvoiceManageService{

	@Autowired
	private InvoiceManageMapper invoiceManageMapper;
	
	@Override
	@Transactional
	public Long insert(InvoiceManageEntity invoice) {
		return invoiceManageMapper.insert(invoice);
	}

	@Override
	@Transactional
	public Long update(InvoiceManageEntity invoice) {
		return invoiceManageMapper.update(invoice);
	}

	@Override
	@Transactional
	public Long delete(Map<String, Object> usmap) {
		return invoiceManageMapper.delete(usmap);
	}

	/*@Override
	public InvoiceControlEntity selectPower(Map<String, Object> usmap) {
		return invoiceManageMapper.selectPower(usmap);
	}*/

	@Override
	@Transactional
	public void insertPower(Map<String, Object> usmap) {
		invoiceManageMapper.insertPower(usmap);
	}

	/*@Override
	@Transactional
	public Long updatePower(InvoiceControlEntity ice) {
		return invoiceManageMapper.updatePower(ice);
	}*/

	@Override
	public void select(PageUtils<InvoiceManageEntity> pageinfo) {
		Long cont = invoiceManageMapper.findInvoiceManageCont(pageinfo);
		pageinfo.setTotal(cont);
		if(cont > 0){
			pageinfo.setData(invoiceManageMapper.findInvoiceManage(pageinfo));
		}
	}

	@Override
	public List<Select> selectAllInvoice(Map<String, Object> usmap) {
		List<InvoiceManageEntity> list = invoiceManageMapper.findInvoiceManageByFacId(usmap);
		return toSelect(list);
	}
	
	public List<Select> toSelect(List<InvoiceManageEntity> list){
		List<Select> invoiceSelect = new ArrayList<Select>();
		Select select = new Select();
		select.setId(Long.valueOf(0));
		select.setLabel("不开发票");
		select.setDiscount(new BigDecimal(0));
		invoiceSelect.add(select);
		
		for(int i = 0;i<list.size();i++){
			Select select1 = new Select();
			select1.setId(list.get(i).getId());
			select1.setLabel(list.get(i).getInvoiceName());
			select1.setDiscount(list.get(i).getTaxRate());
			invoiceSelect.add(select1);
		}
		
		return invoiceSelect;
	}

	/**
	 * 通过ID查询发票信息
	 */
	@Override
	public InvoiceManageEntity find(Long id) {
		return invoiceManageMapper.find(id);
	}

}
